package rxtxsample;


/**
 *
 * @author stefan
 */
import java.io.IOException; 
 
 public class ReaderThread implements Runnable  {
     final static int NMAX = 500;
        Thread treader;
        private SerialComm s;
        private byte rdBuf[] = new byte[NMAX];
        private int rdPos = 0;
        private boolean stop = true;
        
        public void stop() {
        	stop = true;
        }

        public void run() {
        while (!stop) {
            try {Thread.sleep(10);}
            catch (InterruptedException e) {};
                // check for incoming data
            int c;
            try {
                    while ((c = s.readSio()) > -1) 
                          {
                              rdBuf[rdPos] = new Integer(c).byteValue();
                              if (rdPos < NMAX-1) rdPos++;
                              System.out.print(String.format("%c", c));
//                             System.out.print(c);
                          }
                    } // try 
                catch (IOException e) {
                    System.err.println("Exception: " + e.getMessage());
                    } // ioexception
            } // while always
        }

        public ReaderThread(SerialComm aSC) {
            s = aSC;
            System.out.println("Reader Thread started\n");
            treader = new Thread(this, "treader");
            treader.start();
        }
        
        public void join() { 
        	try {treader.join();} catch (Exception e) {
                System.err.println("Exception: " + e.getMessage());
                }
        	} 

}
